package com.game.modular.gameCore.cultivate.service.impl;

import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.game.modular.gameCore.cultivate.mapper.CoreSkillStarMapper;
import com.game.modular.gameCore.cultivate.domain.CoreSkillStar;
import com.game.modular.gameCore.cultivate.service.CoreSkillStarService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;


/**
 * 战法升星表(CoreSkillStar)表服务实现类
 *
 * @author cxy
 * @since 2024-08-05 16:14:02
 */
@Slf4j
@Service
public class CoreSkillStarServiceImpl extends ServiceImpl<CoreSkillStarMapper, CoreSkillStar> implements CoreSkillStarService {

    Cache<String, Map<String,CoreSkillStar>> cache = Caffeine.newBuilder().build();
    
    @PostConstruct
    private void init() {
        CoreSkillStar initSelect = CoreSkillStar.builder().build();
        List<CoreSkillStar> list = this.list(new LambdaQueryWrapper<>(initSelect));
        log.info("开始缓存战法升星表数据,数据量为:{}",list.size());
        cache.put(
                "coreSkillStar", 
                list.stream().collect(Collectors.toMap(item->item.getSkillId()+"_"+item.getSkillStar(), CoreSkillStar -> CoreSkillStar))
        );
        log.info("战法升星表数据缓存完成");
    }

    public List<CoreSkillStar> listCache() {
        Map<String,CoreSkillStar> values = cache.getIfPresent("coreSkillStar");
        if (Objects.isNull(values)) {
            throw new RuntimeException("战法升星表数据为空");
        }
        return new ArrayList<>(values.values());
    }     
    
    public Map<String,CoreSkillStar> mapCache() {
        Map<String,CoreSkillStar> values = cache.getIfPresent("coreSkillStar");
        if (Objects.isNull(values)) {
            throw new RuntimeException("战法升星表数据为空");
        }
        return values;
    }   
    
    public CoreSkillStar getCache(Long skillId,Integer skillStar) {
        Map<String,CoreSkillStar> values = cache.getIfPresent("coreSkillStar");
        if (Objects.isNull(values)) {
            throw new RuntimeException("战法升星表数据为空");
        }
        return values.get(skillId+"_"+skillStar);
    }
    public CoreSkillStar getCache(Long id) {
        List<CoreSkillStar> collect = listCache().stream().filter(value -> value.getId().equals(id)).collect(Collectors.toList());
        if(collect.isEmpty()){
            return  null;
        }
        return collect.get(0);
    }

    public String getProp(Long skillId) {
        return getCache(skillId,1).getLevelUpCost();
    }

    public Integer getByStarId(Long coreSkillStarId) {
        CoreSkillStar coreSkillStar = this.getCache(coreSkillStarId);
        // return coreSkillStarMapper.getByStartId(coreSkillStarId);
        return coreSkillStar.getSkillStar();
    }
}
